<!-- * 文件名：我的资料
* 作者：ZQK
* 描述：〈描述〉
* 修改人：ZQK
* 修改时间：2025-3-9  -->
<template>
	<view class="userInfo">
		<view class="box">
			<view class="content" style="padding: 20rpx 0;">
				<view class="left">
					<text class="title">头像</text>
				</view>
				<view class="right">
					<view class="headerImage" style="margin-right: 10rpx;">
						<button class="avatar-wrapper" open-type="chooseAvatar" @chooseavatar="setAvatar">
							<u-image width="100rpx" height="100rpx" border-radius="50%" :src="userAvatar?userAvatar:getUrl('/missing-face.png')"></u-image>
						</button>
					</view>
					<u-icon name="arrow-right" size="30"></u-icon>
				</view>
			</view>
			
			<view class="content">
				<view class="left">名字</view>
				<view class="right" @click="handleNameShow">
					<text class="rightText" style="margin-right: 10rpx;">{{userInfo.nickName}}</text>
					<u-icon name="arrow-right" size="30"></u-icon>
				</view>
			</view>
			
			<view class="content">
				<view class="left">角色</view>
				<view class="right">
					<text class="rightText">{{userInfo.leaderStatus == '0'?'普通用户':(userInfo.leaderStatus == '1'?'合伙人':'团员')}}</text>
				</view>
			</view>
			
			<view class="content">
				<view class="left">手机号</view>
				<view class="right">
					<text class="rightText">{{userInfo.phonenumber}}</text>
				</view>
			</view>
		</view>
		
		<l-popup
		v-model="popupShow"
		:maskClickAble="false"
		:isShowClose="false"
		height="340rpx">
			<view class="errorPopup">
				<view class="popupTitle">修改名字</view>
				<u-input
					border="surround"
					v-model="nickName"
					placeholder="请输入姓名"
					suffixIconStyle="color: #909399"
					></u-input>
				<view class="popupBtnCouple">
					<view class="popupBtn popupBtn1" @click="popupShow = false">取消</view>
					<view class="popupBtn popupBtn2" @click="nameSubmit">确定</view>
				</view>
			</view>
		</l-popup>
		<!-- <u-popup :show="nameShow" mode="center" round="14">
			<view class="popup">
				<view class="popupName">
					<u-input
						border="surround"
						v-model="nickname"
						placeholder="请输入姓名"
						suffixIconStyle="color: #909399"
						></u-input>
				</view>
				<view class="btn">
					<view class="button buttonLeft" @click="nameShow = false">取消</view>
					<view class="button buttonRight" @click="nameSubmit()">确定</view>
				</view>
			</view>
		</u-popup> -->
	</view>
</template>

<script setup lang="ts">
	import { computed, onMounted, ref } from 'vue';
	import { onShow, onLoad } from "@dcloudio/uni-app"
	import lPopup from '@/components/l-popup/l-popup.vue';
	import { getUrl } from '@/utils/imagePlugin'
	import Api from '@/api/modules/home.ts'
	
	let userInfo = ref<any>({
		userAvatar: '',
		nickName: '',
		phonenumber: '',
	})
	const userAvatar = ref('')
	const nickName = ref('')
	const roleName = ref('')
	const getInfo = () => {
		Api.getInfo().then((res: any) => {
			userInfo.value = res.user
			userAvatar.value = res.user.avatar
			nickName.value = res.user.nickName
			if(res.user.roles.length > 0){
				roleName.value = res.user.roles[0].roleName
			} else {
				roleName.value = '普通用户'
			}
		})
	}
	const setAvatar = (e: any) => {
		uni.getFileInfo({
			
			filePath: e.detail.avatarUrl,
			success: (res: any) => {
				userAvatar.value = e.detail.avatarUrl
				uni.uploadFile({
					url: `${import.meta.env.VITE_APP_AXIOS_BASE_URL}common/upload`,
					filePath: e.detail.avatarUrl,
					name: 'file',
					header:{
						"Authorization": uni.getStorageSync('token')? `Bearer ${uni.getStorageSync('token')}`: ''
					},
					success: (uploadFileRes) => {
						console.log('上传成功回调', JSON.parse(uploadFileRes.data));
						userAvatar.value = JSON.parse(uploadFileRes.data).url
						let params = {
							nickName: nickName.value,
							avatar: userAvatar.value,
						}
						Api.UpdateUser(params).then((res: any) => {
							uni.showToast({ title: '修改成功！', icon: 'none' });
							btnLoading.value = false
							popupShow.value = false
						})
					}
				})
			},
			fail: (err) => {
				console.error('获取文件信息失败:', err);
			}
		});
	}
	
	const popupShow = ref<boolean>(false)
	const handleNameShow = () => {
		popupShow.value = true
	}
	let btnLoading = ref<boolean>(false)
	const nameSubmit = () => {
		if (!nickName.value) {
			uni.showToast({ title: '请输入昵称', icon: 'none' });
			return;
		}
		if(btnLoading.value){
			return;
		}
		btnLoading.value = true
		
		let params = {
			nickName: nickName.value,
			avatar: userAvatar.value,
		}
		Api.UpdateUser(params).then((res: any) => {
			uni.showToast({ title: '修改成功！', icon: 'none' });
			btnLoading.value = false
			popupShow.value = false
			getInfo()
		}).catch(()=>{
			btnLoading.value = false
			popupShow.value = false
		})
	}
	
	const handleToRecruit = () => {
		uni.navigateTo({
			url: '/pages/recruitPartner/recruitPartner'
		})
	}
	
	onLoad(()=>{
		getInfo()
	})
</script>

<style lang="scss" scoped>
	.userInfo{
		display: flex;
		flex-direction: column;
		align-items: center;
		height: 100vh;
		width: 100%;
		position: relative;
		
		.box{
			padding: 0 30rpx;
			width: 100%;
			background-color: #ffffff;
			border-top: 1px solid #E0E0E0;
			
			.content{
				display: flex;
				justify-content: space-between;
				align-items: center;
				border-bottom: 1px solid #E0E0E0;
				padding: 20rpx 0;
				position: relative;
				
				.left{
					display: flex;
					align-items: center;
					font-size: 26rpx;
				}
				
				.right{
					display: flex;
					align-items: center;
					
					.headerImage{
						width: 100rpx;
						height: 100rpx;
						border-radius: 100rpx;
						border: 1rpx solid #cccccc;
						overflow: hidden;
						
						.avatar-wrapper{
							padding: 0 !important;
						}
					}
					
					.addIamge{
						width: 100rpx;
						height: 100rpx;
						/* margin-left: 20rpx; */
						background: #F4F5F7;
						font-size: 60rpx;
						line-height: 100rpx;
						text-align: center;
						border-radius: 10rpx;
					}
					
					.rightText{
						font-size: 24rpx;
						color: #000000;
						// margin-right: 20rpx;
					}
				}
			}
		}
		
		.errorPopup {
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
		
			.popupTitle {
				font-size: 36rpx;
				color: #5EB847;
				font-weight: 600;
				margin-top: 10rpx;
				margin-bottom: 30rpx;
			}
		
			.popupContent {
				color: #483D33;
				margin-bottom: 30rpx;
			}
		
			.popupBtnCouple {
				width: 100%;
				background: #ffffff00;
				padding-bottom: 20rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-top: 20rpx;
		
				.popupBtn {
					display: flex;
					justify-content: center;
					align-items: center;
					height: 70rpx;
					width: 200rpx;
					font-weight: 600;
					font-size: 28rpx;
					margin: 0 auto;
					border: 1rpx solid #5EB847;
					border-radius: 70rpx;
				}
				
				.popupBtn1{
					color: #5EB847;
					background-color: #FFFFFF;
				}
				.popupBtn2{
					color: #FFFFFF;
					background-color: #5EB847;
				}
			}
		}
	}
</style>